package com.core.excel.impl;

import java.math.BigDecimal;

import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.core.cache.Cache;
import com.core.excel.ExcelOperateService;
import com.core.util.DspUtil;
import com.core.util.PinYin;
import com.dsp.bean.Company;
import com.dsp.bean.Employee;
import com.dsp.services.EmployeeService;

public class ExcelEmployeeServiceImpl implements ExcelOperateService {

	@Override
	public List<Map<String, Object>> run(List<Map<String, Object>> data) throws Exception{
		Employee employee = null;
		Map<String, Long> company = this.getCompany();
		EmployeeService employeeService = (EmployeeService) DspUtil.getBean("employeeService");
		for (Map<String, Object> map : data) {
			employee = new Employee();
			employee.setEmpname(DspUtil.req(map.get("empname"), ""));
			employee.setPyCode(new PinYin().toPinYin(employee.getEmpname(), true, false));
			employee.setEmpidn(DspUtil.req(map.get("empidn"), ""));
			employee.setBargainBegin(DspUtil.reqDate(map.get("bargainBegin"), new Date(System.currentTimeMillis())));
			employee.setBargainEnd(DspUtil.reqDate(map.get("bargainEnd"), Date.valueOf("2099-12-31")));
			employee.setJob(DspUtil.req(map.get("job"), ""));
			employee.setcMethod(DspUtil.req(map.get("cMethod"), ""));
			employee.setInsureBase(new BigDecimal(DspUtil.req(map.get("insureBase"), "0")));
			employee.setProvidentBase(new BigDecimal(DspUtil.req(map.get("providentBase"), "0")));
			employee.setTelphone(DspUtil.req(map.get("telphone"), ""));
			employee.setInsureCard(DspUtil.req(map.get("insureCard"), ""));
			employee.setLoseCard(DspUtil.req(map.get("loseCard"), ""));
			employee.setSex(DspUtil.reqInteger(this.getSelectTag("sex", DspUtil.req(map.get("sex"), "0"))));
			employee.setIsInsure(DspUtil.reqInteger(this.getSelectTag("insure", DspUtil.req(map.get("isInsure"),"0"))));
			employee.setIsProvident(DspUtil.reqInteger(this.getSelectTag("insure", DspUtil.req(map.get("isProvident"),"0"))));
			employee.setNational(DspUtil.reqInteger(this.getSelectTag("edu", DspUtil.req(map.get("national"), "0"))));
			employee.setEducation(DspUtil.reqInteger(this.getSelectTag("edu", DspUtil.req(map.get("education"), "0"))));
			employee.setCompany(company.get(DspUtil.req(map.get("company"), "0")));
			employee.setHukou(DspUtil.reqInteger(this.getSelectTag("hukou", DspUtil.req(map.get("hukou"), "0"))));
			employee.setJobBegin(DspUtil.reqDate(map.get("jobBegin"), new Date(System.currentTimeMillis())));
			employeeService.save(employee);
		}
		return null;
	}

	/**
	 * 得到下拉框的值
	 * 
	 * @param tagId
	 * @param value
	 * @return
	 */
	private String getSelectTag(String tagId, String value) {
		Map<String, List<Map<String, String>>> select = Cache.getSelect();
		List<Map<String, String>> list = select.get(tagId);
		for (Map<String, String> m : list) {
			if (value.equalsIgnoreCase(m.get("name"))) {
				return DspUtil.req(m.get("value"), "0");
			}
		}
		return "0";
	}

	/**
	 * 得到公司信息
	 * 
	 * @return
	 */
	private Map<String, Long> getCompany() {
		Map<String, Company> map = Cache.getCompany();
		Map<String, Long> value = new HashMap<String, Long>();
		for (Iterator<Map.Entry<String, Company>> iter = map.entrySet().iterator(); iter.hasNext();) {
			Map.Entry<String, Company> entry = (Map.Entry<String, Company>) iter.next();
			value.put(entry.getValue().getCname(), DspUtil.reqLong(entry.getKey()));
		}
		return value;
	}
}
